package com.lwb.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lwb.eduservice.entity.EduChapter;
import com.lwb.eduservice.entity.EduVideo;
import com.lwb.eduservice.entity.chapter.ChapterVo;
import com.lwb.eduservice.entity.chapter.VideoVo;
import com.lwb.eduservice.mapper.EduChapterMapper;
import com.lwb.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwb.eduservice.service.EduVideoService;
import com.lwb.serviceBase.StudyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-17
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;//注入小节service

    @Override
    public List<ChapterVo> getChapterVideoById(String courseId) {
        //1. 根据课程id查询所有的章节
        QueryWrapper<EduChapter> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        queryWrapper1.orderByAsc("sort");
        List<EduChapter> eduChapters = baseMapper.selectList(queryWrapper1);

        //2. 根据课程id查询所有的小节
        QueryWrapper<EduVideo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("course_id",courseId);
        queryWrapper2.orderByAsc("sort");
        List<EduVideo> eduVideos = videoService.list(queryWrapper2);

        //创建集合用于封装
        List<ChapterVo> list = new ArrayList<>();

        //3.封装chapterVo集合
        for (int i=0;i<eduChapters.size();i++){
            EduChapter eduChapter = eduChapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);

            //创建集合用于封装章节中的小节
            List<VideoVo> videoVos = new ArrayList<>();

            //4. 遍历封装VideoVo集合
            for (int j=0;j<eduVideos.size();j++){
                EduVideo eduVideo = eduVideos.get(j);
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVos.add(videoVo);
                }
            }

            chapterVo.setChildren(videoVos);
            list.add(chapterVo);
        }
        return list;
    }

    @Override
    public boolean deleteChapterById(String chapterId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("chapter_id",chapterId);
        int count = videoService.count(queryWrapper);
        if(count>0){
            throw new StudyException(20001,"不能删除");
        }else{
            int delete = baseMapper.deleteById(chapterId);
            return delete>0;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        int delete = baseMapper.delete(queryWrapper);
    }
}
